<template>
    <div ref="domNode" tabIndex="0" class="sc-emoji-picker" @blur="onBlur">
        <div class="sc-emoji-picker--content">
            <div
                v-for="category in emojiData"
                :key="category.name"
                class="sc-emoji-picker--category"
            >
                <div class="sc-emoji-picker--category-title">{{ category.name }}</div>
                <span
                    v-for="emoji in category.emojis"
                    :key="emoji"
                    class="sc-emoji-picker--emoji"
                    @click="emojiClicked(emoji)"
                >
                    {{ emoji }}
                </span>
            </div>
        </div>
    </div>
</template>

<script>
import EmojiConvertor from 'emoji-js'
import emojiData from './emojiData'

export default {
    props: {
        onBlur: {
            type: Function,
            required: true,
        },
        onEmojiPicked: {
            type: Function,
            required: true,
        },
    },
    data() {
        return {
            emojiData,
            emojiConvertor: new EmojiConvertor(),
        }
    },
    mounted() {
        const elem = this.$refs.domNode
        elem.style.opacity = 0
        window.requestAnimationFrame(() => {
            elem.style.transition = 'opacity 350ms'
            elem.style.opacity = 1
        })
        this.$refs.domNode.focus()
        this.emojiConvertor.init_env()
    },
    methods: {
        emojiClicked(emoji) {
            this.onEmojiPicked(emoji)
            this.$refs.domNode.blur()
        },
    },
}
</script>

<style scoped>
.sc-emoji-picker {
    position: absolute;
    bottom: 180px;
    left: 6px;
    width: 380px;
    max-height: 215px;
    box-shadow: 0px 2px 12px 0px #ffffff;
    border-radius: 4px;
    background: white;
    outline: none;
    z-index: 19;
}

.sc-emoji-picker--content {
    padding: 10px;
    overflow: auto;
    width: 100%;
    max-height: 195px;
    margin-top: 7px;
    box-sizing: border-box;
}

.sc-emoji-picker--content::-webkit-scrollbar {
    width: 6px; /* 修改宽度 */
    border-radius: 8px;
}

/* 修改滚动条轨道背景色 */
.sc-emoji-picker--content::-webkit-scrollbar-track {
    background-color: #f1f1f1;
}

/* 修改滚动条滑块颜色 */
.sc-emoji-picker--content::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 12px;
}

/* 修改滚动条滑块悬停时的颜色 */
.sc-emoji-picker--content::-webkit-scrollbar-thumb:hover {
    background-color: #555;
}

.sc-emoji-picker--category {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}

.sc-emoji-picker--category-title {
    min-width: 100%;
    color: #b8c3ca;
    font-weight: 200;
    font-size: 13px;
    margin: 5px;
    letter-spacing: 1px;
}

.sc-emoji-picker--emoji {
    margin: 5px;
    width: 15px;
    line-height: 15px;
    text-align: center;
    cursor: pointer;
    vertical-align: middle;
    font-size: 16px;
    transition: transform 60ms ease-out;
}

.sc-emoji-picker--emoji:hover {
    transform: scale(1.4);
}
</style>
